package service.impl;

import model.User;
import service.UserManager;
import dao.UserDao;
import java.util.*;

public class UserManagerImpl implements UserManager {
	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public int addUser(String user, String pass, String identity,String email,String realName,String gender,String tel,int power) throws Exception {
		// TODO Auto-generated method stub
		try {
			User u = new User();
			u.setUser(user);
			u.setPass(pass);
			u.setIdentity(identity);
			u.setEmail(email);
			u.setRealName(realName);
			u.setGender(gender);
			u.setTel(tel);
			u.setPower(power);
			userDao.save(u);
			return u.getId();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("新增用户时出现异常");
		}

	}

	public int loginValid(String user, String pass) throws Exception {
		// TODO Auto-generated method stub
		try {
			User u = userDao.findUserByNameAndPass(user, pass);
			if (u != null) {
				return u.getId();
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("验证用户登录时出现异常");
		}
		return -1;
	}

	public boolean validateName(String name) throws Exception {
		// TODO Auto-generated method stub
		try {
			User u = userDao.findUserByName(name);
			if (u == null) {
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("验证用户是否有效是出现异常");
		}

	}
	public User getUser(int id) throws Exception {
		// TODO Auto-generated method stub
		try {
			User u = userDao.get(id);
			return u;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("查找用户时出现异常");
		}	
	}
	public List getAllUser() throws Exception{
		try {
			System.out.print("aaaa");
			return userDao.findAll();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new Exception("查找所有用户时出现异常");
		}
	
	}
}
